<?php
class UserController extends Core\Api{
    public function userAction(){
        $user_id = $this->session->user['id'];
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $data = Business\UserModel::getUser($user_id);
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    public function commentAction(){
        $user_id = $this->session->user['id'];
        $per_page = $this->post('per_page',10);
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $res = Business\UserModel::getCommentList($user_id,$per_page);
        $ret = $this->ok($res['data']);
        $ret['page'] = $res['page'];
        return $this->json($ret);
    }
    public function walletAction() {
        $user_id = $this->session->user['id'];
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $res = Business\UserModel::getWallet($user_id);
        $ret = $this->ok($res);
        return $this->json($ret);
    }
    public function billAction(){
        $user_id = $this->session->user['id'];
        $per_page = $this->post('per_page',10);
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $res = Business\UserModel::getBillList($user_id,$per_page);
        $ret = $this->ok($res['data']);
        $ret['page'] = $res['page'];
        return $this->json($ret);
    }
    public function orderAction() {
        $user_id = $this->session->user['id'];
        $pay_status = $this->post('pay_status',null);
        $per_page = $this->post('per_page',10);
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $res = Business\UserModel::getOrderList($user_id,$pay_status,$per_page);
        $ret = $this->ok($res['data']);
        $ret['page'] = $res['page'];
        return $this->json($ret);
    }
    public function giftcardAction() {
        $user_id = $this->session->user['id'];
        $is_use = $this->post('is_use',null);
        $is_expire = $this->post('is_expire',null);
        $per_page = $this->post('per_page',10);
        if (!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $res = Business\UserModel::getGiftcardList($user_id,$is_use,$is_expire,$per_page);
        $ret = $this->ok($res['data']);
        $ret['page'] = $res['page'];
        return $this->json($ret);
    }
    //使用充值卡充值 包含B和C类充值
    public function inpourFromGiftcardAction(){
        $code = $this->post('code',null);
        $type = $this->post('type','C');
        $user_id = $this->session->user['id'];
        if (!$code) {
            $ret = $this->error('充值卡密码错误');
            return $this->json($ret);
        }
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $res = Business\UserModel::inpourFromGiftcard($user_id,$code,$type);
        if (!$res) {
            $ret = $this->error('充值卡不存在,或者已经过期');
            return $this->json($ret);
        }
        $ret = $this->ok();
        return $this->json($ret);
    }
    public function inpourFromThirdpartyAction(){
        $money = $this->post('money',0);
        $pay_type = $this->post('pay_type','alipay');
        $user_id = $this->session->user['id'];

        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        if (!$money>0) {
            $ret = $this->error('至少充值0元以上哦');
            return $this->json($ret);
        }
        $res = Business\UserModel::inpourFromThirdparty($user_id,$money,$pay_type);
        if (!$res) {
            $ret = $this->error('充值准备失败');
            return $this->json($ret);
        }
        $ret = $this->ok($res);
        return $this->json($ret);

    }
    public function addFriendAction(){
        $user_id = $this->session->user['id'];
        $friend_id = $this->post('friend_id',null);
        $friend_search = $this->post('friend_search',null);
        if (!$friend_id && !$friend_search) {
            $ret = $this->error('参数错误');
            return $this->json($ret);
        }
        if (!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $res = Business\UserModel::addFriend($user_id,$friend_id,$friend_search);
        if ($res === 0) {
             $ret = $this->error('好友不存在');
            return $this->json($ret);
        }
        if ($res === 2) {
             $ret = $this->error('好友已存在');
            return $this->json($ret);
        }
        $ret = $this->error('添加成功');
        $ret = $this->ok();
        return $this->json($ret);
    }
    public function friendAction(){
        $user_id = $this->session->user['id'];
        if (!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $friend_nickname = $this->post('friend_nickname',null);
        $res = Business\UserModel::friend($user_id,$friend_nickname);
        $ret = $this->ok($res);
        return $this->json($ret);
    }
    public function tellToFriendAction(){
        $user_id = $this->session->user['id'];
        if (!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $from_user_id = $this->post('from_user_id');
        $to_user_id = $this->post('to_user_id');
        $product_id = $this->post('product_id');
        $res = Business\UserModel::tellToFriend($from_user_id,$to_user_id,$product_id);
        $ret = $this->ok($res);
        return $this->json($ret);
    }
    public function messageAction() {
        $user_id = $this->session->user['id'];
        if (!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $type = $this->post('type');
        $per_page = $this->post('per_page');
        $res = Business\UserModel::message($user_id,$type,$per_page);
        $ret = $this->ok($res);
        return $this->json($ret);
    }
}
